package com.jsb.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.annotation.SystemServiceLog;
import com.jsb.common.config.Const;
import com.jsb.common.config.Global;
import com.jsb.common.utils.JsonUtil;
import com.jsb.mapper.AnswerMapper;
import com.jsb.mapper.QuestionMapper;
import com.jsb.mapper.UserMapper;
import com.jsb.model.Answer;
import com.jsb.model.AnswerExample;
import com.jsb.model.AnswerExample.Criteria;
import com.jsb.model.Question;
import com.jsb.model.User;

@Service
public class AnswerService{
	
	@Resource
	private AnswerMapper answerMapper;
	
	@Resource
	private UserMapper userMapper;
	
	@Resource
	private QuestionMapper questionMapper;
	
	@Resource
	private RedisService redisService;
	
	public void addAnswer(Answer answer) {
		answerMapper.insertSelective(answer);
	}
	
	public void delAnswerByUuid(String uuid) {
		answerMapper.deleteByPrimaryKey(uuid);
	}
	
	public void updateAnswer(Answer answer) {
		answerMapper.updateByPrimaryKeySelective(answer);
	}
	
	public void updateAnswerByExample(Answer answer,AnswerExample answerExample){
	    answerMapper.updateByExampleSelective(answer,answerExample);
	}
	
	public Answer getAnswerByUuid(String uuid) {
		return answerMapper.selectByPrimaryKey(uuid);
	}
	
	public List<Answer> getPageAnswer(AnswerExample answerExample) {
		return answerMapper.selectPage(answerExample);
	}
	
	public int countAnswer(AnswerExample answerExample) {
		return answerMapper.countByExample(answerExample);
	}
	
	public List<Answer> selectByExample(AnswerExample example){
	    return answerMapper.selectByExample(example);
	}
	/**
	 * 审核举报通过
	 * @param uuid
	 * @param id
	 * @return
	 */
	public String passReport(String uuid, String id) {
		Map<String,String> map = new HashMap<String, String>();
		Answer answer1 = answerMapper.selectByPrimaryKey(uuid);
		String phoneNumber = answer1.getPhoneNumber();
		Answer answer = new Answer();
		answer.setUuid(uuid);
		answer.setStatus(Integer.valueOf(Const.PASSREPORT));
		answer.setAuditTime(new Date());
		answer.setAuditor(id);
		try {
			answerMapper.updateByPrimaryKeySelective(answer);
			int num = reportNum(phoneNumber);
			if(num >= Const.COUNT){
				freezeUser(phoneNumber);
			}
			freezeUser(uuid);             
			map.put("status", Global.TRUE);
			map.put("info", "审核成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", Global.FALSE);
			map.put("info", "系统繁忙");
		}
		return JsonUtil.toJson(map);
	}
	/**
	 * 用户被举报次数
	 * @param phoneNumber
	 * @return
	 */
	public int reportNum(String phoneNumber){
		AnswerExample example = new AnswerExample();
		Criteria criteria = example.createCriteria();
		criteria.andPhoneNumberEqualTo(phoneNumber);
		criteria.andStatusEqualTo(Integer.valueOf(Const.REPORTED));
		int count = answerMapper.countByExample(example);
		return count;
	}
	/**
	 * 冻结用户
	 * @param phoneNumber
	 */
	public void freezeUser(String phoneNumber){
			//冻结用户
			User user = new User();
			user.setRole(Integer.valueOf(Const.ROLE_FREEZE));
			user.setPhoneNumber(phoneNumber);
			userMapper.updateByPrimaryKeySelective(user);
	}
	/**
	 * 隐藏回答
	 * @param uuid
	 */
	@SystemServiceLog(description = "隐藏回答")
	public void updateHidden(String uuid) {
		Answer selectByPrimaryKey = answerMapper.selectByPrimaryKey(uuid);
		String delFlag = selectByPrimaryKey.getDelFlag();
		if(delFlag.equals(Global.HIDE)){
			return;
		}
		Answer answer = new Answer();
		answer.setUuid(uuid);
		answer.setDelFlag(Global.HIDE);
		answerMapper.updateByPrimaryKeySelective(answer);
		
		String questionUuid = selectByPrimaryKey.getQuestionUuid();
		Question question = questionMapper.selectByPrimaryKey(questionUuid);
		if(null != question){
			Integer answerNum = question.getAnswerNum();
			Question q = new Question();
			q.setUuid(questionUuid);
			q.setAnswerNum(answerNum - 1);
			questionMapper.updateByPrimaryKeySelective(q);
			
			redisService.decrby(questionUuid+"_num");  //redis 中回答数量减1
		}else{
			return;
		}
	}	
	/**
	 * 显示回答
	 */
	public void updateShow(String uuid){
		Answer selectByPrimaryKey = answerMapper.selectByPrimaryKey(uuid);
		String delFlag = selectByPrimaryKey.getDelFlag();
		if(delFlag.equals(Global.SHOW)){
			return;
		}
		Answer answer = new Answer();
		answer.setUuid(uuid);
		answer.setDelFlag(Global.SHOW);
		answerMapper.updateByPrimaryKeySelective(answer);
		String questionUuid = selectByPrimaryKey.getQuestionUuid();
		Question question = questionMapper.selectByPrimaryKey(questionUuid);
		if(null != question){
			Integer answerNum = question.getAnswerNum();
			Question q = new Question();
			q.setUuid(questionUuid);
			q.setAnswerNum(answerNum + 1);
			questionMapper.updateByPrimaryKeySelective(q);
			
			redisService.incrby(questionUuid+"_num");  //redis 中回答数量加1
		}else{
			return;
		}
	}
}
